package com.yanqu.road.dao.impl.monopolymarket;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.config.MonopolyMarketCrossConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/12 15:59
 */
public class MonopolyMarketCrossConfigDaoImpl extends TempDataObjectDao<MonopolyMarketCrossConfig> {

    public MonopolyMarketCrossConfig getMonopolyMarketCrossConfig(int channelId){
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_c_monopolymarket_cross_config where `channelId` = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, channelId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parseData(rs);
            }
            rs = call.executeQuery();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateMonopolyMarketCrossConfigSql = "update `t_c_monopolymarket_cross_config` set mapSize=?,mapBase=?,mapAdd=?,mapRound=?,mapMidValue=?,resConfig=? ,bloodVolume=? where channelId=? ";
    @Override
    protected boolean updateData(MonopolyMarketCrossConfig monopolyMarketCrossConfig) {
        monopolyMarketCrossConfig.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapSize()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapBase()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapAdd()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapRound()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapMidValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketCrossConfig.getResConfig()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketCrossConfig.getBloodVolume()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getChannelId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketCrossConfigSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketCrossConfigSql = "insert into `t_c_monopolymarket_cross_config`(channelId,mapSize,mapBase,mapAdd,mapRound,mapMidValue,resConfig,bloodVolume) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketCrossConfig monopolyMarketCrossConfig) {
        monopolyMarketCrossConfig.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getChannelId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapSize()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapBase()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapAdd()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapRound()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossConfig.getMapMidValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketCrossConfig.getResConfig()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketCrossConfig.getBloodVolume()));
        if (getDb().executeNoneQuery(insertMonopolyMarketCrossConfigSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketCrossConfig parseData(ResultSet rs) throws SQLException {
        MonopolyMarketCrossConfig monopolyMarketCrossConfig = new MonopolyMarketCrossConfig();
        monopolyMarketCrossConfig.setChannelId(rs.getInt("channelId"));
        monopolyMarketCrossConfig.setMapSize(rs.getInt("mapSize"));
        monopolyMarketCrossConfig.setMapBase(rs.getInt("mapBase"));
        monopolyMarketCrossConfig.setMapAdd(rs.getInt("mapAdd"));
        monopolyMarketCrossConfig.setMapRound(rs.getInt("mapRound"));
        monopolyMarketCrossConfig.setMapMidValue(rs.getInt("mapMidValue"));
        monopolyMarketCrossConfig.setResConfig(rs.getString("resConfig"));
        monopolyMarketCrossConfig.setBloodVolume(rs.getString("bloodVolume"));

        // 解析extendParam
        monopolyMarketCrossConfig.setResExtendParamMap(MonopolyMarketBackEndConfigDaoImpl.parseExtendParam(monopolyMarketCrossConfig.getResConfig()));
        monopolyMarketCrossConfig.setNpcBloodConfigList(MonopolyMarketBackEndConfigDaoImpl.parseNpcBloodConfig(monopolyMarketCrossConfig.getBloodVolume()));

        return monopolyMarketCrossConfig;
    }
}
